from medical import models
from django.core.exceptions import ValidationError
from django import forms
from medical.utils.bootstrap import BootStrapModelForm


# 登录组件
class LoginModelForm(BootStrapModelForm):
    class Meta:
        model = models.users
        fields = ['user_name', 'user_pwd']
    user_name = forms.CharField(
        label="用户名",
        widget=forms.TextInput,
        required=True
    )
    user_pwd = forms.CharField(
        label="密码",
        widget=forms.PasswordInput(render_value=True),
        required=True
    )

    code = forms.CharField(
        label="验证码",
        widget=forms.TextInput,
        required=True
    )

    # def clean_user_pwd(self):
    #     pwd = self.cleaned_data.get("user_pwd")
    #     return pwd

#
# # 管理员组件
# class Users_addModelForm(BootStrapModelForm):
#     confirm_pwd = forms.CharField(
#         label="确认密码",
#         widget=forms.PasswordInput(render_value=True),
#     )
#
#     class Meta:
#         model = models.users
#         fields = ["user_id", "user_name", "user_pwd", "confirm_pwd", "user_real_name", "user_email", "user_tel"]
#
#     def clean_user_tel(self):
#         txt_tel = self.cleaned_data["user_tel"]
#         # 验证手机号存在
#         print(self.instance.pk)
#         exists = models.users.objects.exclude(user_id=self.instance.pk).filter(user_tel=txt_tel).exists()
#         if exists:
#             raise ValidationError("手机号已经存在")
#         # return txt_mobile
#
#         elif len(txt_tel) != 11:
#             # 验证不通过
#             raise ValidationError("手机号码格式错误")
#         # 验证通过，用户输入的值返回
#         else:
#             return txt_tel
#
#     def clean_user_email(self):
#         txt_email = self.cleaned_data["user_email"]
#         # 验证手机号存在
#         print(self.instance.pk)
#         exists = models.users.objects.exclude(user_id=self.instance.pk).filter(user_email=txt_email).exists()
#         if exists:
#             raise ValidationError("Email已经存在")
#         # return txt_email
#         else:
#             return txt_email
#
#     def clean_confirm_pwd(self):
#         pwd = self.cleaned_data.get("user_pwd")
#         confirm = self.cleaned_data.get("confirm_pwd")
#         if confirm != pwd:
#             raise ValidationError("密码不一致")
#         # 返回什么，此字段以后保存到数据库就是什么。
#         return confirm
#
#
# class Users_editModelForm(BootStrapModelForm):
#     class Meta:
#         model = models.users
#         fields = ["user_name", "user_pwd", "user_real_name", "user_email", "user_tel"]
#
#     def clean_user_tel(self):
#         txt_tel = self.cleaned_data["user_tel"]
#         # 验证手机号存在
#         print(self.instance.pk)
#         exists = models.users.objects.exclude(user_id=self.instance.pk).filter(user_tel=txt_tel).exists()
#         if exists:
#             raise ValidationError("手机号已经存在")
#         # return txt_mobile
#
#         elif len(txt_tel) != 11:
#             # 验证不通过
#             raise ValidationError("手机号码格式错误")
#         # 验证通过，用户输入的值返回
#         else:
#             return txt_tel
#
#     def clean_user_email(self):
#         txt_email = self.cleaned_data["user_email"]
#         # 验证手机号存在
#         print(self.instance.pk)
#         exists = models.users.objects.exclude(user_id=self.instance.pk).filter(user_email=txt_email).exists()
#         if exists:
#             raise ValidationError("Email已经存在")
#         # return txt_email
#         else:
#             return txt_email


# 医院
class HospitalModelForm(BootStrapModelForm):
    class Meta:
        model = models.hospital
        fields = ["hos_id", "hos_name", "hos_province", "hos_city", "hos_email"]


# 次科室表
class SubModelForm(BootStrapModelForm):
    class Meta:
        model = models.sub_department
        fields = ["sub_dep_id", "sub_dep_name", "main_dep"]
